package com.woyowodaows.activity.mine;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.woyowodaows.R;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.base.BaseBean;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.EditUtil;
import com.woyowodaows.utils.LogUtils;
import com.woyowodaows.utils.RegexpUtils;
import com.woyowodaows.utils.RegularUtils;
import com.woyowodaows.utils.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 *  个人信息编辑
 *  2017/7/17
 */

public class PersonManagerActivity extends BaseActivity {

    @BindView(R.id.tv_person_item)
    TextView item;
    @BindView(R.id.et_person_info)
    EditText info;
    @BindView(R.id.bt_person_sure)
    Button sure;
    @BindView(R.id.tv_nickname)
    TextView tvNickName;//昵称内容
    private String mark;
    private String infoStr;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0://设置昵称
                    Intent intent = new Intent();
                    intent.putExtra("nick",info.getText().toString().trim());
                    setResult(5,intent);
                    finish();
                    break;
                case 4://设置姓名
                    Intent pivRealNameIntent = new Intent();
                    pivRealNameIntent.putExtra("RealName",info.getText().toString().trim());
                    setResult(2,pivRealNameIntent);
                    finish();
                    break;
                case 8:
                    ToastUtil.show_short(PersonManagerActivity.this,"修改失败!");
                    break;
                case 9:
                    ToastUtil.show_short(PersonManagerActivity.this,"没有权限");
                    break;
                case 7:
                    Intent emailIntent = new Intent();
                    emailIntent.putExtra("rlEmail",info.getText().toString().trim());
                    setResult(8,emailIntent);
                    finish();
                    break;


            }
        }
    };



    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        mark = getIntent().getExtras().getString("MARK");
        infoStr = getIntent().getExtras().getString("INFORMATION");

        if (mark.equals("昵称")){
            setStyle("修改昵称","请填写你的昵称",mark,infoStr);
            tvNickName.setVisibility(View.VISIBLE);
        }

        if(mark.equals("设置姓名")){
            setStyle("设置姓名","请填写你的真实姓名",mark,infoStr);
            info.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
        }
        if (mark.equals("设置邮箱")){
            setStyle("编辑邮箱","请输入您的常用邮箱",mark,infoStr);
            info.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS);
        }
        info.setSelection(info.getText().length());
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_person_edit;
    }

    private void setStyle(String titleText,String hint,String itemText,String infoStr){
        setTitleBar(false,titleText,null,0,0,null);
        item.setText(itemText);
        EditUtil.setHint(info,hint);
        if (infoStr != null && !infoStr.isEmpty() && !infoStr.equals("未设置")){
            info.setText(infoStr);
        }

    }

    @OnClick(R.id.bt_person_sure)
    public void onSure(Button button){
        String str = info.getText().toString().trim();
        if (mark.equals("昵称")){
            LogUtils.d("length",str.length()+"--");
            if (TextUtils.isEmpty(str)){
                info.setError("昵称不能为空");
            }else if (str.length()<2 || str.length() >8){
                info.setError("请输入2-8个字符");
            }else if (!RegularUtils.isHZ(str)){
                info.setError("由字母和汉字组成！");
            }else {
                editNikeName(str);
            }
        }
        //设置姓名
        if(mark.equals("设置姓名")){
            if (TextUtils.isEmpty(str)){
                info.setError("真实姓名不能为空");
            }else if (!(RegularUtils.isChinese(str) || RegularUtils.isEnglish(str))){
                info.setError("姓名由汉字组成！");
            }else {
                editUserName(str);
            }
        }
        if (mark.equals("设置邮箱")) {
            if (TextUtils.isEmpty(str)) {
                info.setError("邮箱不能为空");
            } else if (!RegexpUtils.isEmail(str)) {
                info.setError("邮箱格式不正确！");
            } else {
                editEmail(str);
            }

        }

    }

    private void editUserName(String realname) {
        //        fieldName:email   // email 电子邮箱格式       gender:1男 2女       realname 真实姓名必须是中文     birthday 请确认你输入生日是否正确    nickname:昵称不能为空  avatar: 头像路径不能为空
//        fieldValue:332741160@qq.com
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("fieldName", "realname");
        mapParameter.put("fieldValue", realname);//昵称
        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.UPDATEUSERINFO, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        if (handler != null) {
                            handler.sendEmptyMessage(4);
                            ToastUtil.show(mContext, bean.getMessage());
                        }
                    } else {
                        ToastUtil.show(mContext, bean.getMessage());
                    }
                }
            }

            @Override
            public void onError(String message) {
                ToastUtil.show(mContext, message);

            }
        });


    }

    //编辑昵称
    private void editNikeName(String nikeName) {
//        fieldName:email   // email 电子邮箱格式       gender:1男 2女       realname 真实姓名必须是中文     birthday 请确认你输入生日是否正确    nickname:昵称不能为空  avatar: 头像路径不能为空
//        fieldValue:332741160@qq.com
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("fieldName", "nickname");
        mapParameter.put("fieldValue", nikeName);//昵称
        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.UPDATEUSERINFO, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        if (handler != null) {
                            handler.sendEmptyMessage(0);
                            ToastUtil.show(mContext, bean.getMessage());
                        }
                    } else {
                        ToastUtil.show(mContext, bean.getMessage());
                    }
                }
            }

            @Override
            public void onError(String message) {
                ToastUtil.show(mContext, message);

            }
        });
    }


        //编辑邮箱

    private void editEmail(String email) {
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("fieldName", "email");//类型
        mapParameter.put("fieldValue", email);//值
//        fieldName:email   // email 电子邮箱格式       gender:1男 2女       realname 真实姓名必须是中文     birthday 请确认你输入生日是否正确    nickname:昵称不能为空  avatar: 头像路径不能为空
//        fieldValue:332741160@qq.com
        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.UPDATEUSERINFO, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        if (handler != null) {
                            handler.sendEmptyMessage(7);
                            ToastUtil.show(mContext, bean.getMessage());
                        }
                    } else {
                        ToastUtil.show(mContext, bean.getMessage());
                    }
                }
            }

            @Override
            public void onError(String message) {
                ToastUtil.show(mContext, message);

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

}

